from google.appengine.ext import db
from google.appengine.ext.db import polymodel

class User(polymodel.PolyModel):
    created = db.DateTimeProperty(auto_now_add=True)
    modified = db.DateTimeProperty(auto_now=True)
    nickname = db.StringProperty(required=True)
    avatar = db.ReferenceProperty()
    profile = db.StringProperty(required=True)
    
class ForumUser(User):
    group = db.StringProperty(default = '')
    posts = db.IntegerProperty(default = 0)
    days = db.IntegerProperty(default = 0)
    type = db.StringProperty(default = 'forum')

class GoogleUser(User):
    user = db.UserProperty()
    type = db.StringProperty(default = 'google')

class BaseEntity(polymodel.PolyModel):
    created = db.DateTimeProperty(auto_now_add=True)
    modified = db.DateTimeProperty(auto_now=True)
    creator = db.ReferenceProperty(User)
    visible = db.BooleanProperty(default=True)
    short_name = db.StringProperty(required = True)
    name = db.StringProperty(default = '', required = True)
    description = db.TextProperty(default = '')
    icon = db.ReferenceProperty()
    videos = db.ListProperty(db.Link)
    links = db.ListProperty(db.Link)
    tags = db.StringListProperty()

class Reqs(db.Model):
    min_posts = db.IntegerProperty(default=0)
    min_days = db.IntegerProperty(default=0)
    groups = db.StringListProperty(default=(''))
    maximum = db.IntegerProperty(default=1)

class Rules(db.Model):
    created = db.DateTimeProperty(auto_now_add=True)
    modified = db.DateTimeProperty(auto_now=True)
    description = db.TextProperty()
    submission_reqs = db.ReferenceProperty(Reqs, collection_name = 'rules_submission_map')
    vote_reqs = db.ReferenceProperty(Reqs, collection_name = 'rules_vote_map')
    hidden = db.BooleanProperty(default=False)

class Series(BaseEntity):
    rules = db.ReferenceProperty(Rules)

class Contest(BaseEntity):
    series = db.ReferenceProperty(Series)
    prizes = db.StringListProperty()
    state = db.StringProperty(choices = ('ANNOUNCED','OPEN','VOTING','FINISHED'),default = 'ANNOUNCED')
    contest_open = db.DateTimeProperty()
    contest_closed = db.DateTimeProperty()
    voting_closed = db.DateTimeProperty()

class Submission(BaseEntity):
    contest = db.ReferenceProperty(Contest)
    reply_to = db.SelfReferenceProperty()

class Winner(db.Model):
    submission = db.ReferenceProperty(Submission)
    contest = db.ReferenceProperty(Contest)
    place = db.IntegerProperty()
    mention = db.StringProperty()

class Vote(db.Model):
    creator = db.ReferenceProperty(User)
    created = db.DateTimeProperty(auto_now_add=True)
    submission = db.ReferenceProperty(Submission)
    rating = db.RatingProperty(required = True)

class Image(polymodel.PolyModel):
    creator = db.ReferenceProperty(User)
    created = db.DateTimeProperty(auto_now_add=True)
    object = db.ReferenceProperty(BaseEntity)
    url = db.StringProperty()
    caption = db.StringProperty()

class UploadedImage(Image):
    data = db.BlobProperty()
    type = db.StringProperty()

class LinkedImage(Image):
    original_url = db.StringProperty()
